package pers.muci.es.doc;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.Arrays;

/**
 * @ClassName DocInsert
 * @Description TODO
 * @Author muci
 * @Date 2021/12/21 21:59
 * @Version 1.0
 **/
public class DocInsertBatch {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("106.14.15.56", 9200, "http"))
        );

        //批量插入
        BulkRequest request = new BulkRequest();
        IndexRequest first = new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "first", "age", 30, "sex", "男");
        IndexRequest second = new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "second", "age", 30, "sex", "女");
        IndexRequest third = new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "third", "age", 40, "sex", "男");
        IndexRequest four = new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "four", "age", 40, "sex", "女");
        IndexRequest five = new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "five", "age", 50, "sex", "男");
        IndexRequest six = new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name", "six", "age", 50, "sex", "男");
        request.add(Arrays.asList(first, second, third, four, five, six));
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.getItems());
        System.out.println(response.getTook());

        client.close();
    }
}
